import javax.management.Notification;
import javax.management.NotificationListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ConnectWindow extends JFrame {
	
	// Don't change
	private static final long serialVersionUID = -8409426778035607503L;
	
	int HEIGHT = 500;
	int WIDTH  = 500;
	
	ConnectMenu		MenuBar			= new ConnectMenu();
	JPanel			MasterPanel		= new JPanel( new CardLayout() );
	WelcomePanel	WelcomeScreen;
	StartPanel		StartScreen;
	GamePanel		GameScreen;
	GameOverPanel	GameOverScreen;
	// add an end game screen???
	
	public ConnectWindow(){
		setJMenuBar(MenuBar);
		
		getContentPane().add(MasterPanel);
		WelcomeScreen = new WelcomePanel(ClickedScreen);
		StartScreen = new StartPanel(SizeListener);
		GameOverScreen = new GameOverPanel(ClickedScreen);
		MasterPanel.add(WelcomeScreen, "Welcome");
		MasterPanel.add(StartScreen, "Start");
		
		
		setTitle("Connect4!");
		setSize(WIDTH,HEIGHT);
		setResizable(false);
		setLocationRelativeTo(null); //centers window
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	* Listeners to call the JPanels to change, must be explicitly called when needed
	**/
	
	// To change from welcome screen to set up screen
	MouseListener ClickedScreen = new MouseListener(){
		public void mouseClicked(MouseEvent arg0) {}
		public void mouseEntered(MouseEvent arg0) {}
		public void mouseExited(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {}

		public void mouseReleased(MouseEvent arg0) {
			// Switch to GameScreen
			CardLayout cl = (CardLayout)(MasterPanel.getLayout());
			cl.next(MasterPanel);
		}
		
	};
	
	// Used to change from setup screen to game board
	NotificationListener SizeListener = new NotificationListener(){
		public void handleNotification(Notification notification, Object handback) {
			//JOptionPane.showMessageDialog(null, "Notified!");
			
			int SetUpInfo[] = (int[])handback;
			
			// Set up GameScreen
			GameScreen = new GamePanel( SetUpInfo, GameOver_BoardFull );
			MasterPanel.add(GameScreen, "Game");
			
			
			// Switch to GameScreen
			CardLayout cl = (CardLayout)(MasterPanel.getLayout());
	        cl.next(MasterPanel);
		}
		
	};
	
	// Game board to end game screen
	NotificationListener GameOver_BoardFull = new NotificationListener(){
		public void handleNotification(Notification notification, Object handback) {
			//set up GameOverScreen
			MasterPanel.add(GameOverScreen,"End");
			if (handback != null)
				GameOverScreen.label2.setText((String)handback);
			// Switch to GameOverScreen
			CardLayout cl = (CardLayout)(MasterPanel.getLayout());
	        cl.next(MasterPanel);
		}
		
	};
	
	
	// MAIN <may be moved to separate file...>
	public static void main( String[] args ){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				ConnectWindow win1 = new ConnectWindow();
				win1.setVisible(true);
			}
		});
	}
	
}
